package com.itany.manager.controller;

import com.itany.manager.constant.DictConstant;
import com.itany.manager.entity.User;
import com.itany.manager.response.ApiResponse;
import com.itany.manager.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Min;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping
    public ApiResponse save(@RequestBody @Valid User user) {
        return ApiResponse.success(userService.saveUser(user));
    }

    @PatchMapping("/{id}")
    public ApiResponse delete(@PathVariable Integer id) {
        return ApiResponse.success(userService.removeById(id));
    }

    @PutMapping
    public ApiResponse update(@RequestBody User user) {
        return ApiResponse.success(userService.updateById(user));
    }

    @GetMapping("/{id}")
    public ApiResponse getById(@PathVariable Integer id) {
        return ApiResponse.success(userService.getById(id));
    }

    @GetMapping
    public ApiResponse list(@RequestParam(defaultValue = DictConstant.DEFAULT_NO) @Min(1) Long pageNo,
                            @RequestParam(defaultValue = DictConstant.DEFAULT_PAGE) @Min(1) Long pageSize,
                             User user) {
        System.out.println(user);
        return ApiResponse.success(userService.getUsersByPage(pageNo, pageSize, user));
    }
}
